package com.example.source.java.jdbc;

import java.sql.*;

public class JdbcDemo {
    public static void main(String[] args) throws Exception{
        //1.导入jar包
        //2.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //3.获取连接 （连接的数据库名是db2，第二个第三个参数是连接数据库的用户名密码）
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/study?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false","root","root");

        //4.获取执行者对象 （statement：表现，声明，跟程序意思不匹配）
        Statement stat = conn.createStatement();

        //5.执行sql语句，并且接收结果
        String sql = "SELECT * FROM PERSON";
        ResultSet rs = stat.executeQuery(sql); //execute执行，query：查询，resultset：结果集

        //6.处理结果
        while(rs.next()) {
            System.out.println(rs.getInt("age") + "\t" + rs.getString("firstname"));
        }

        PreparedStatement preparedStatement = conn.prepareStatement("SELECT * FROM PERSON where age > ?");
        preparedStatement.setInt(1,20);
        ResultSet rs1 = preparedStatement.executeQuery();
        while(rs.next()) {
            System.out.println(rs.getInt("age") + "\t" + rs.getString("firstname"));
        }
        //7.释放资源
        conn.close();
        stat.close();
        conn.close();
    }
}
